﻿package mortal.game.view.lotteryDraw
{
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.geom.*;
    import mortal.game.manager.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.common.*;
    import mortal.game.view.guide.*;

    public class lotteryDrawFlyEffect extends Object
    {
        private var _callback:Function;
        private var _tweens:Array;
        private var _curBmps:Array;
        private var _guideFlyEff:GuideFlyEffect;
        public static var instance:lotteryDrawFlyEffect = new lotteryDrawFlyEffect;

        public function lotteryDrawFlyEffect()
        {
            this._tweens = [];
            this._curBmps = [];
            this._guideFlyEff = new GuideFlyEffect();
            return;
        }// end function

        public function work(param1:Point, param2:Point, param3:Array, param4:int = 180, param5:Function = null, param6:Boolean = false) : void
        {
            var _loc_7:int = 0;
            var _loc_9:GBitmap = null;
            var _loc_10:TimelineLite = null;
            var _loc_11:int = 0;
            var _loc_12:int = 0;
            var _loc_13:Number = NaN;
            var _loc_14:int = 0;
            var _loc_15:int = 0;
            var _loc_16:Number = NaN;
            var _loc_17:Number = NaN;
            if (param3 == null || param3.length == 0)
            {
                return;
            }
            this._callback = param5;
            if (param6 && this._tweens != null && this._tweens.length > 0)
            {
                return;
            }
            this.dispose();
            _loc_7 = param3.length;
            var _loc_8:int = 0;
            while (_loc_8 < _loc_7)
            {
                
                _loc_9 = this.getBmp(param3[_loc_8] as ItemData);
                LayerManager.highestLayer.addChild(_loc_9);
                _loc_10 = this.getLite(_loc_8);
                this._curBmps.push(_loc_9);
                _loc_11 = (Math.random() - 0.5) * 2 * param4;
                _loc_12 = (Math.random() - 0.5) * 2 * param4;
                _loc_9.x = param1.x;
                _loc_9.y = param1.y;
                var _loc_18:Number = 0.5;
                _loc_9.scaleY = 0.5;
                _loc_9.scaleX = _loc_18;
                _loc_13 = 0.6 + Math.random() * 0.4;
                _loc_14 = (Math.random() - 0.5) * param1.y * 1.2 + param1.y;
                _loc_15 = (Math.random() - 0.5) * param2.x * 1.2 + param2.x;
                _loc_17 = 0.9;
                if (_loc_8 == (_loc_7 - 1))
                {
                    _loc_10.append(TweenLite.to(_loc_9, _loc_17, {x:param1.x + _loc_11, y:param1.y + _loc_12, scaleX:_loc_13, scaleY:_loc_13, ease:Back.easeOut}));
                    _loc_10.append(TweenLite.to(_loc_9, 0.5, {x:param2.x + Math.random() * 0.5 * 10, y:param2.y + Math.random() * 0.5 * 10, scaleX:0.7, scaleY:0.7, bezier:[{x:_loc_15, y:_loc_14}], onComplete:this.tweenEnd}));
                }
                else
                {
                    _loc_16 = 0.5 + Math.random() * 0.4;
                    _loc_10.append(TweenLite.to(_loc_9, _loc_16, {x:param1.x + _loc_11, y:param1.y + _loc_12, scaleX:_loc_13, scaleY:_loc_13, ease:Back.easeOut}));
                    _loc_10.append(TweenLite.to(_loc_9, 0.5, {x:param2.x + Math.random() * 0.5 * 10, y:param2.y + Math.random() * 0.5 * 10, scaleX:0.7, scaleY:0.7, bezier:[{x:_loc_15, y:_loc_14}]}));
                }
                _loc_8++;
            }
            this._guideFlyEff.work(new Point(param1.x + 10, param1.y + 10), new Point(param2.x + 10, param2.y + 10), _loc_7 * 2, param4);
            return;
        }// end function

        private function tweenEnd() : void
        {
            this.dispose();
            if (this._callback != null)
            {
                this._callback.apply();
                this._callback = null;
            }
            return;
        }// end function

        public function dispose() : void
        {
            var _loc_1:TimelineLite = null;
            var _loc_2:GBitmap = null;
            if (this._tweens != null && this._tweens.length > 0)
            {
                for each (_loc_1 in this._tweens)
                {
                    
                    if (_loc_1 != null && _loc_1.active)
                    {
                        _loc_1.kill();
                        _loc_1.clear();
                    }
                }
            }
            this._tweens = [];
            if (this._curBmps != null && this._curBmps.length > 0)
            {
                for each (_loc_2 in this._curBmps)
                {
                    
                    if (_loc_2.parent != null)
                    {
                        _loc_2.parent.removeChild(_loc_2);
                    }
                    _loc_2.dispose(true);
                }
            }
            this._curBmps = [];
            return;
        }// end function

        private function getLite(param1:int) : TimelineLite
        {
            var _loc_2:* = this._tweens[param1];
            if (_loc_2 == null)
            {
                _loc_2 = new TimelineLite();
                this._tweens[param1] = _loc_2;
            }
            return _loc_2;
        }// end function

        private function getBmp(param1:ItemData) : GBitmap
        {
            var onToAnyPointItemLoaded:Function;
            var onLoadIconFailed:Function;
            var url:String;
            var value:* = param1;
            onToAnyPointItemLoaded = function (param1:ImageInfo) : void
            {
                (param1.extData as GBitmap).bitmapData = param1.bitmapData;
                return;
            }// end function
            ;
            onLoadIconFailed = function () : void
            {
                MsgManager.showRollTipsMsg(Language.getString(90425));
                return;
            }// end function
            ;
            var res:* = UIFactory.gBitmap(null, 0, 0, LayerManager.highestLayer);
            if (value && value.itemInfo && value.itemInfo.icon)
            {
                url = value.itemInfo.icon + ".jpg";
                LoaderManager.instance.load(url, onToAnyPointItemLoaded, 3, res, null, onLoadIconFailed);
            }
            return res;
        }// end function

    }
}
